import router from './router'
import store from './store'
import WPopView from '@/utils/popView'
import WLog from '../../utils/YBLog'

const wPoint = () => {}
// const wPoint = console.log

router.beforeEach((to, from, next) => {
  wPoint('to = ', to)
  wPoint('beforeEach -> 0')
  if (store.getters.token) {
    wPoint('beforeEach -> 1')
    if (to.path === '/login') {
      wPoint('beforeEach -> 2')
      next({ name: 'AdminDashboard' })
    } else {
      wPoint('beforeEach -> 3')
      if (store.getters.user.name) {
        wPoint('beforeEach -> 4')
        next()
      } else {
        wPoint('beforeEach -> 5')
        store.dispatch('GET_USER_INFO').then(() => { // 拉取用户信息
          wPoint('beforeEach -> 6')
          next()
        }).catch(() => {
          wPoint('beforeEach -> 7')
          store.dispatch('FED_LOG_OUT')
          next({ path: '/' })
        })
      }
    }
  } else {
    wPoint('beforeEach -> 9')
    if (to.meta.auth) {
      wPoint('beforeEach -> 10')
      next('/')
    } else {
      wPoint('beforeEach -> 11')
      next()
    }
  }
})

router.beforeEach(function (to, from, next) {
  WPopView.showLoading()
  next()
})

router.afterEach(function (to) {
  WLog.addProcess(to)
  WPopView.hideLoading()
})
